|
|
|
|
|
|
|
Think of a collection as being similar to, but not exactly like, an array. You can access it like an array, as in the following line of code, which sets a form's picture to the first image in an ImageList control: |
|
|
|
|
|
|
|
|
Set form1.Picture = ImageList1.ListImages(1).Picture |
|
|
|
|
|
|
|
|
Notice that a specific object is referred to with its indexin this case, 1. Remember, a collection stores objects, unlike an array, which stores values. These objects have their own set of properties. |
|
|
|
|
|
|
|
|
Objects in a Buttons collection have a special property: the Key property. A Button object's key is a text string that can be used in the same manner as an index: |
|
|
|
|
|
|
|
|
Set form1.Picture = ImageList1.ListImages("Smiley Face").Picture |
|
|
|
|
|
|
|
|
Generally, the Button object's Key property is set when the object is added to the Buttons collection through the key parameter of the Buttons collection's Add method. |
|
|
|
|
|
|
|
|
To set up a ToolBar control in code, you have to first set up an ImageList control (either in design mode or with code), including populating it with images. Then you can assign the ImageList control to the toolbar with a statement like the following: |
|
|
|
|
|
|
|
|
Set tbrMain.ImageList = ImageList1 |
|
|
|
|
|
|
|
|
Next, you manipulate the toolbar's Buttons collection to create toolbar buttons. In the following example, a separator and standard button are added to a toolbar control, tbrMain: |
|
|
|
|
|
|
|
|
tbrMain.Buttons.Add 1, "Sep1",, tbrSeparator, 0
tbrMain.Buttons.Add 2, "open","Open File" , tbrDefault, 1 |
|
|
|
|
|
|
|
|
In this example, two additional Button objects are created in the Buttons collection by using the Add method. The first two parameters are the button's index and key, each of which must be unique. The third parameter is the button caption. The fourth parameter (tbrSeparator and tbrDefault) is a constant representing the type of button. Finally, an index value from the associated image list tells the toolbar which picture to use. |
|
|
|
|
|
|
|
|
The Add method can also work as a function. If used in this manner, it returns a reference to the Button object just created. You could rewrite the preceding code like this: |
|
|
|
|
|
|
|
|
Dim btn As Button
Set btn = tbrMain.Buttons.Add(, "Sep1", , tbrSeparator, 0)
Set btn = tbrMain.Buttons.Add(,"open", , tbrDefault)
btn.ToolTipText = "Click to open a file"
btn.Caption = "Open File"
btn.Image = 1 |
|
|
|
|
|
|
|
|
Note that the ToolTipText property must be added to the Button object because it is not in the Add method's parameters. Also, in the preceding example, the Index properties are defined automatically because they are not specified. This approach is generally the preferred way of coding because, as objects are added and deleted from a collection, the index of a particular object will change. The Key, however, is always associated with a specific object. |
|
|
|
|
|